<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require("base_c.php");

class travel_c extends base_c {

	public function index()
	{
		$subcategory_id = $this->uri->segment(2);
		$category_slug = $this->uri->segment(1);	
		$category_id = $this->category_m->getIdBySlug($category_slug)->id;
		$category_name = $this->category_m->getIdBySlug($category_slug)->name;

		if($subcategory_id != "" && $subcategory_id != "page") 
		{
			$subcategory_id = end(explode('-', $subcategory_id));
			$subcategory_name	= $this->subcategory_m->getItem($subcategory_id)->name;
			$subcategory_slug	= $this->subcategory_m->getItem($subcategory_id)->slug;

			$this->data['category_name'] = $subcategory_name;

			$total = $this->base_m->getTotal('news', 0, $category_id, $subcategory_id);
			$page_current = $this->uri->segment(4);
			if (!$page_current) {
				$page_current = 0;
			}
			$config['base_url'] = base_url(). $category_slug."/".$subcategory_slug."-".$subcategory_id."/page";
			$config['total_rows'] = $total;
			$config['per_page'] = 10;
			$config['uri_segment'] = 4;
			$config['full_tag_open'] = '<div class="pagination">';
			$config['full_tag_close'] = '</div>';
			$this->pagination->initialize($config);

			$this->data['articleItems'] = $this->base_m->getPaging('news', 0, $category_id, $subcategory_id, $page_current, $config['per_page']);
		}
		else if ($subcategory_id == "" || $subcategory_id == "page")
		{
			$subcategory_id = 0;
			$this->data['category_name'] = $category_name;

			$total = $this->base_m->getTotal('news', 0, $category_id, $subcategory_id);
			$page_current = $this->uri->segment(3);
			if (!$page_current) {
				$page_current = 0;
			}
			$config['base_url'] = base_url(). $category_slug ."/page";
			$config['total_rows'] = $total;
			$config['per_page'] = 10;
			$config['uri_segment'] = 3;
			$config['full_tag_open'] = '<div class="pagination">';
			$config['full_tag_close'] = '</div>';
			$this->pagination->initialize($config);

			$this->data['articleItems'] = $this->base_m->getPaging('news', 0, $category_id, $subcategory_id, $page_current, $config['per_page']);
		}

		$this->data['subcategory_id'] = $subcategory_id;
		$this->data['page'] = 'travel';

		$this->data['title'] = $category_name;
		$this->data['keywords'] = '';
		$this->data['description'] = '';
		$this->load->view('layout', $this->data);
	}

	public function item_detail()
	{
		$category_slug = $this->uri->segment(1);
		$subcategory_id = $this->uri->segment(2);

		if(is_numeric($subcategory_id)) 
		{
			$id_seg = end(explode('-', $this->uri->segment(3)));

			$this->data['category_name'] = $this->category_m->getIdBySlug($category_slug)->name;
			$this->data['category_link'] = base_url().$category_slug;

			$subcategory_slug = $this->subcategory_m->getItem($subcategory_id)->slug;
			$this->data['subcategory_name'] = $this->subcategory_m->getItem($subcategory_id)->name;
			$this->data['subcategory_link'] = base_url().$category_slug."/".$subcategory_slug."-".$subcategory_id;

			$travel_item = $this->base_m->getNews('news', $id_seg, 0, 0, 0, 1, '', 0);
			$this->data['travel_item'] = $travel_item;
			$this->data['related_news_items'] = $this->base_m->getRelatedNews('news', $travel_item->id, $travel_item->category_id, $travel_item->subcategory_id, 5);

			$this->data['page'] = 'travel-detail';

			$this->data['title'] = $travel_item->name;
			$this->data['keywords'] = '';
			$this->data['description'] = mb_substr(strip_tags($travel_item->content),  0, 256, "utf-8");
			$this->load->view('layout', $this->data);
		}
	}

	public function travel_search()
	{
		$keyword = $_POST['key'];
		$area_id = $_POST['area'];
		$city_id = $_POST['city'];

		$this->data['articleItems'] = $this->base_m->getNewsSearch($keyword, $area_id, $city_id, 0);
		$this->load->view('partials/travel-list', $this->data);
	}

	public function travel_3mien()
	{
		$this->data['page'] = 'travel';

		$mien = $this->uri->segment(2);
		$id_mien = "";
		$category_name = "";
		if($mien == "mien-bac") 
		{
			$id_mien = 1;
			$category_name = "Du lịch miền Bắc";
		}
		if($mien == "mien-trung") 
		{
			$id_mien = 2;
			$category_name = "Du lịch miền Trung";
		}
		if($mien == "mien-nam") 
		{
			$id_mien = 3;
			$category_name = "Du lịch miền Nam";
		}

		$this->data['articleItems'] = $this->base_m->getNewsSearch('', $id_mien, 0, 0);
		$this->data['category_name'] = $category_name;

		$this->data['title'] = 'Du lich Mien Bac';
		$this->data['keywords'] = '';
		$this->data['description'] = '';
		$this->load->view('layout', $this->data);
	}

}